package org.freez4.spring.bean.definition;

import org.freez4.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 注解方式注册Bean 元信息有以下几个方式
 * <pre>
 * 1. @Component
 * 2. @Bean
 * 3. @Import
 * 4. XML方式
 * 5. JAVA API
 * 命名方式: {@link BeanDefinitionRegistry#registerBeanDefinition(String, BeanDefinition)}
 * 非命名方式: {@link org.springframework.beans.factory.support.BeanDefinitionReaderUtils#registerWithGeneratedName(AbstractBeanDefinition, BeanDefinitionRegistry)}
 * 配置类方式: {@link org.springframework.context.annotation.AnnotatedBeanDefinitionReader#register(Class[])}
 * </pre>
 *
 * @author 阿坤同学
 * @date 2023/12/6 4:08 PM
 */
@Import(AnnotationBeanDefinitionDemo.Config.class)
public class AnnotationBeanDefinitionDemo {

    private static final Logger logger = Logger.getLogger(AnnotationBeanDefinitionDemo.class.getName());

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(AnnotationBeanDefinitionDemo.class);
        applicationContext.refresh();

        User user = applicationContext.getBean("user", User.class);
        logger.log(Level.INFO, () -> "通过注解注入UserBean: " + user);
        applicationContext.close();
    }


    @Configuration
    public static class Config {
        @Bean
        public User user() {
            User user = new User();
            user.setId(2);
            user.setAge(23);
            user.setName("小明");
            return user;
        }
    }

}
